<?php


namespace Xiaoguai\DesignPattern\DP014\Mediator;


use Xiaoguai\DesignPattern\DP014\Colleague;
use Xiaoguai\DesignPattern\DP014\MediatorInterface;

class Mediator implements MediatorInterface
{

    /**
     * @var Colleague[] $colleagues
     */
    protected $colleagues = [];

    public function register(Colleague $colleague): void
    {
        $this->colleagues[] = $colleague;
    }

    public function relay(Colleague $colleague): void
    {
        foreach ($this->colleagues as $registered_colleague) {
            if ($registered_colleague !== $colleague) {
                $registered_colleague->receive();
            }
        }
    }
}